package bo;

import IBO.IAdresse;
import IBO.IClient;
import IBO.IIntervention;
import IBO.ITechnicien;
import boerrors.AdresseInexistantException;
import boerrors.AdresseMinimumMultiplicityException;
import boerrors.ClientInexistantException;
import boerrors.CodePostalMinimumMultiplicityException;
import boerrors.InterventionExistantException;
import boerrors.InterventionInexistantException;
import boerrors.RoleMinimumMultiplicityException;
import boerrors.TechnicienMinimumMultiplicityException;
import boerrors.ZoneMinimumMultiplicityException;
import dao.AbstractFactory;
import dao.dto.AdresseDto;
import dao.dto.ClientParticulierDto;
import dao.dto.CodePostalDto;
import dao.dto.InterventionDto;
import dao.dto.PrioriteDto;
import dao.dto.TechnicienDto;
import dao.dto.ZoneDto;
import dao.impl.FactoryImpl;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import run.PublisherImpl;
import util.Util;
 
public class Intervention extends PublisherImpl implements IIntervention, Serializable, Cloneable{
    
        //Differentes Etat de l'intervention
        private final InterventionEtat _cree = new InterventionCree();
        private final InterventionEtat _encours = new InterventionEnCours();
        private final InterventionEtat _planifiee = new InterventionPlanifiee();
        private final InterventionEtat _deplanifiee = new InterventionDeplanifiee();
        private final InterventionEtat _suivis = new InterventionSuivis();
        private final InterventionEtat _cloturee = new InterventionCloturee();
        private final InterventionEtat _annulee = new InterventionAnnulee();

        //Demande demande intervention
        private int _idIntervention;
        private InterventionEtat _statut;
        private Client _client;
        private Adresse _adresse;
        private String _descriptionDemande;
        private String _prioriteDemande;
        private Intervention _parent;
	private GregorianCalendar _dateDemande;

        //Planification d une intervention
	private GregorianCalendar _dateIntervention;
	private Technicien _technicien;
	private String _prioriteIntervention;
        
        //Traitement d une intervention
	private GregorianCalendar _heureDebut;
	private GregorianCalendar _heureFin;
	private String _descriptionIntervention;
        private Intervention _enfant;

        //*******************************************************************    
        //CONSTRUCTEUR  
        //******************************************************************* 
        
        public Intervention(Client client, Adresse adresse, String descriptionDemande, 
                            String prioriteDemande, GregorianCalendar dateDemande) throws RemoteException {
                try {
                    this.setInterventionEtatcree();
                    this.enregistrerClient(client);
                    this.enregistrerAdresse(adresse);
                    this.setDescriptionDemande(descriptionDemande);
                    this.setPrioriteDemande(prioriteDemande);
                    this.setDateDemande(dateDemande);
                    this.setIdIntervention(FactoryImpl.getInstance().getInterventionDao().enregistrer(creerInterventiondto()));
                    ListeInterventions.getInstance().ajouterIntervention(this);
                    //UnicastRemoteObject.exportObject(this, 0);
                } catch (InterventionExistantException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (AdresseInexistantException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (CodePostalMinimumMultiplicityException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ZoneMinimumMultiplicityException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
        
        // sous intervention
        public Intervention(Intervention interventionAnc, String descriptionDemande, 
                            String prioriteDemande) throws RemoteException {
            
            //intervention,descriptionIntervention, GregorianCalendar heureFin, descriptionDemande, degre
            
                try {
                    // nouvelle
                    this.setInterventionEtatcree();
                    this.enregistrerClient(interventionAnc.getClient());
                    this.enregistrerAdresse(interventionAnc.getAdresse());
                    this.setDescriptionDemande(descriptionDemande);
                    this.setPrioriteDemande(prioriteDemande);
                    this.setDateDemande(interventionAnc.getHeureFin());
                    InterventionDto interventionDto = this.creerInterventiondto();
                    
                     // cloturer ancienne
                    InterventionDto interventionDtoAnc = interventionAnc.creerInterventiondto();
                    interventionDtoAnc.setDescriptionIntervention(interventionAnc.getDescriptionIntervention());
                    interventionDtoAnc.setHeureFin(interventionAnc.getHeureFin());
                    
                    this.setIdIntervention(FactoryImpl.getInstance().getInterventionDao().suivi(interventionDtoAnc,creerInterventiondto()));
                    this.setInterventionEtatcloturee();
                    
                    ListeInterventions.getInstance().ajouterIntervention(this);
                    ListeInterventions.getInstance().supprimerIntervention(interventionAnc);
                    
                    //UnicastRemoteObject.exportObject(this, 0);
                } catch (ClientInexistantException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (InterventionInexistantException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (AdresseInexistantException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (CodePostalMinimumMultiplicityException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ZoneMinimumMultiplicityException ex) {
                    Logger.getLogger(Intervention.class.getName()).log(Level.SEVERE, null, ex);
                }
        }
        
        
        public Intervention(InterventionDto intervention) throws RemoteException {
                this.setIdIntervention(intervention.getIdIntervention());
                this.setDateDemande(intervention.getDateDemande());
                this.setDescriptionDemande(intervention.getDescriptionDemande());
                this.enregistrerAdresse(new Adresse(intervention.getAdresse()));
                String demandeTmp = intervention.getPrioriteDemande().getPriorite();
                if (demandeTmp.compareTo("F") == 0) {
                    this.setPrioriteDemande("Faible");
                } else if (demandeTmp.compareTo("M") == 0) {
                    this.setPrioriteDemande("Moyen");
                } else if (demandeTmp.compareTo("U") == 0) {
                    this.setPrioriteDemande("Urgent");
                }

                
                if (intervention.getStatut().getStatut().contentEquals("CR")) {
                    this.setInterventionEtatcree();
                    this.enregistrerClient(ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient()));
                }
                if (intervention.getStatut().getStatut().contentEquals("DE")) {
                    String interventionTmp = intervention.getPrioriteIntervention().getPriorite();
                    if (interventionTmp.compareTo("F") == 0) {
                        this.setPrioriteIntervention("Faible");
                    } else if (interventionTmp.compareTo("M") == 0) {
                       this.setPrioriteIntervention("Moyen");
                    } else if (interventionTmp.compareTo("U") == 0) {
                        this.setPrioriteIntervention("Urgent");
                    }
                    this.setDateIntervention(intervention.getDateIntervention());
                    this.setInterventionEtatdeplanifiee();
                    this.enregistrerClient(ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient()));
                }
                if (intervention.getStatut().getStatut().contentEquals("PL")) {
                    String interventionTmp = intervention.getPrioriteIntervention().getPriorite();
                    if (interventionTmp.compareTo("F") == 0) {
                        this.setPrioriteIntervention("Faible");
                    } else if (interventionTmp.compareTo("M") == 0) {
                       this.setPrioriteIntervention("Moyen");
                    } else if (interventionTmp.compareTo("U") == 0) {
                        this.setPrioriteIntervention("Urgent");
                    }
                    this.setInterventionEtatplanifiee();
                    this.setDateIntervention(intervention.getDateIntervention());
                    this.enregistrerTechnicien(ListeTechniciens.getInstance().rechercherEmploye(intervention.getTechnicien().getMatricule()));
                    this.enregistrerClient(ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient()));
                }
                if (intervention.getStatut().getStatut().contentEquals("EN")) {
                    String interventionTmp = intervention.getPrioriteIntervention().getPriorite();
                    if (interventionTmp.compareTo("F") == 0) {
                        this.setPrioriteIntervention("Faible");
                    } else if (interventionTmp.compareTo("M") == 0) {
                       this.setPrioriteIntervention("Moyen");
                    } else if (interventionTmp.compareTo("U") == 0) {
                        this.setPrioriteIntervention("Urgent");
                    }
                    this.setDateIntervention(intervention.getDateIntervention());
                    this.setHeureDebut(intervention.getHeureDebut());
                    this.setInterventionEtatencours();
                    this.enregistrerTechnicien(ListeTechniciens.getInstance().rechercherEmploye(intervention.getTechnicien().getMatricule()));
                    this.enregistrerClient(ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient()));
                }
                
                if (intervention.getStatut().getStatut().contentEquals("AN")) {
                    String interventionTmp = intervention.getPrioriteIntervention().getPriorite();
                    if (interventionTmp.compareTo("F") == 0) {
                        this.setPrioriteIntervention("Faible");
                    } else if (interventionTmp.compareTo("M") == 0) {
                       this.setPrioriteIntervention("Moyen");
                    } else if (interventionTmp.compareTo("U") == 0) {
                        this.setPrioriteIntervention("Urgent");
                    }
                    this.setDateIntervention(intervention.getDateIntervention());
                    this.setInterventionEtatannulee();
                    if(intervention.getTechnicien() != null)
                        this._technicien = ListeTechniciens.getInstance().rechercherEmploye(intervention.getTechnicien().getMatricule());
                    this._client = ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient());
                }
                
                if (intervention.getStatut().getStatut().contentEquals("CL")) {
                    String interventionTmp = intervention.getPrioriteIntervention().getPriorite();
                    if (interventionTmp.compareTo("F") == 0) {
                        this.setPrioriteIntervention("Faible");
                    } else if (interventionTmp.compareTo("M") == 0) {
                       this.setPrioriteIntervention("Moyen");
                    } else if (interventionTmp.compareTo("U") == 0) {
                        this.setPrioriteIntervention("Urgent");
                    }
                    this.setDateIntervention(intervention.getDateIntervention());
                    this.setHeureDebut(intervention.getHeureDebut());
                    this.setHeureFin(intervention.getHeureFin());
                    this.setDescriptionIntervention(intervention.getDescriptionIntervention());
                    this.setInterventionEtatcloturee();
                    this._technicien = ListeTechniciens.getInstance().rechercherEmploye(intervention.getTechnicien().getMatricule());
                    this._client = ListeClients.getInstance().rechercherIdClient(intervention.getClient().getIdClient());
                }
                    
                //UnicastRemoteObject.exportObject(this, 0);
        }
        
        //Intervention enfant
        public Intervention(Client client, Adresse adresse, Intervention parent, GregorianCalendar dateDemande) throws RemoteException {
                enregistrerClient(client);
                enregistrerAdresse(adresse);
                enregistrerParent(parent);
                setDateDemande(dateDemande);
                //UnicastRemoteObject.exportObject(this, 0);
        }
        
        //*******************************************************************
        //Convertir bo - dto
        //*******************************************************************
        
        private InterventionDto creerInterventiondto() throws   RemoteException, AdresseInexistantException, 
                                                                CodePostalMinimumMultiplicityException, 
                                                                ZoneMinimumMultiplicityException {
                ZoneDto zoneTampon = new ZoneDto(   this.getAdresse().getCodePostal().getZone().getIdZone(), 
                                                    this.getAdresse().getCodePostal().getZone().getLibelleZone());
                CodePostalDto codePostalTampon = new CodePostalDto( this.getAdresse().getCodePostal().getIdCodePostal(), 
                                                                    this.getAdresse().getCodePostal().getCodePostal(),
                                                                    this.getAdresse().getCodePostal().getVille(), 
                                                                    this.getAdresse().getCodePostal().getPays(),zoneTampon);
                AdresseDto tampon = new AdresseDto( this.getAdresse().getIdAdresse(), this.getAdresse().getRue(), 
                                                    this.getAdresse().getNumero(), codePostalTampon);
                ClientParticulierDto clientdto = new ClientParticulierDto(  this._client.getIdClient(),this._client.getNom(), 
                                                                            this._client.getPrenom(), this._client.getNumeroTva(),
                                                                            tampon,this._client.getTelephone(), this._client.getEmail());
                PrioriteDto prioriteDto = new PrioriteDto(String.valueOf(_prioriteDemande.charAt(0)).toUpperCase());
                return new InterventionDto(this._idIntervention, clientdto, tampon, this._dateDemande, this._descriptionDemande, prioriteDto);
        }
        
        private InterventionDto creerInterventionPlanifieedto() throws   RemoteException, AdresseInexistantException, 
                                                                CodePostalMinimumMultiplicityException, 
                                                                ZoneMinimumMultiplicityException {
                ZoneDto zoneTampon = new ZoneDto(   this.getAdresse().getCodePostal().getZone().getIdZone(), 
                                                    this.getAdresse().getCodePostal().getZone().getLibelleZone());
                CodePostalDto codePostalTampon = new CodePostalDto( this.getAdresse().getCodePostal().getIdCodePostal(), 
                                                                    this.getAdresse().getCodePostal().getCodePostal(),
                                                                    this.getAdresse().getCodePostal().getVille(), 
                                                                    this.getAdresse().getCodePostal().getPays(),zoneTampon);
                AdresseDto tampon = new AdresseDto( this.getAdresse().getIdAdresse(), this.getAdresse().getRue(), 
                                                    this.getAdresse().getNumero(), codePostalTampon);
                ClientParticulierDto clientdto = new ClientParticulierDto(  this._client.getIdClient(),this._client.getNom(), 
                                                                            this._client.getPrenom(), this._client.getNumeroTva(),
                                                                            tampon,this._client.getTelephone(), this._client.getEmail());
                PrioriteDto prioriteDto = new PrioriteDto(String.valueOf(_prioriteDemande.charAt(0)).toUpperCase());
                return new InterventionDto(this.getIdIntervention(), clientdto, tampon, this._dateDemande, this._descriptionDemande, prioriteDto);
        }
        
        //*******************************************************************
	//Setters
	//*******************************************************************
        
        public synchronized void setIdIntervention(int idIntervention) {
		Util.checkPositive(idIntervention);
                this._idIntervention = idIntervention;
	}
	
        public synchronized void setStatut(InterventionEtat statut) {
		Util.checkObject(statut);
                this._statut = statut;
	}

        public synchronized void setDescriptionDemande(String descriptionDemande) {
		Util.checkString(descriptionDemande);
                this._descriptionDemande = descriptionDemande;
	}

        public synchronized void setPrioriteDemande(String prioriteDemande) {
		Util.checkString(prioriteDemande);
                this._prioriteDemande = prioriteDemande;
	}

        public synchronized void setDateDemande(GregorianCalendar dateDemande) {
		this._dateDemande = dateDemande;
	}
	
       	public synchronized void setDateIntervention(GregorianCalendar dateIntervention) {
		this._dateIntervention = dateIntervention;
	}

	public synchronized void setPrioriteIntervention(String prioriteIntervention) {
                this._prioriteIntervention = prioriteIntervention;
        }
        
	public synchronized void setHeureDebut(GregorianCalendar heureDebut) {
		this._heureDebut = heureDebut;
	}

	public synchronized void setHeureFin(GregorianCalendar heureFin) {
		this._heureFin = heureFin;
	}

        public synchronized void setDescriptionIntervention(String descriptionIntervention) {
		Util.checkString(descriptionIntervention);
                this._descriptionIntervention = descriptionIntervention;
	}

        //*******************************************************************
        //Getters 
	//*******************************************************************

        public synchronized int getIdIntervention() throws RemoteException {
		return _idIntervention;
	}
	
	public synchronized InterventionEtat getStatut() throws RemoteException {
		return _statut;
	}

        public synchronized  String getIStatut() throws RemoteException {
            return this._statut.getlibelle();
        }
        
        public synchronized Client getClient() throws RemoteException, ClientInexistantException {
                if(!aUnClientValide()) throw new ClientInexistantException();
        return _client;
        }
    
        public synchronized IClient getIClient() throws RemoteException, ClientInexistantException {
                if(!aUnClientValide()) throw new ClientInexistantException();
        return _client;
        }        
        
        public synchronized Adresse getAdresse()throws RemoteException, AdresseInexistantException {
                if (!aUneAdresseValide()) throw new AdresseInexistantException();
                return _adresse;
        }        
    
        public synchronized IAdresse getIAdresse() throws RemoteException, AdresseInexistantException {
                if (!aUneAdresseValide()) throw new AdresseInexistantException();
                return _adresse;
        }        
        
        public synchronized String getDescriptionDemande() throws RemoteException {
		return _descriptionDemande;
	}

        public synchronized String getPrioriteDemande() throws RemoteException {
		return _prioriteDemande;
	}

        public synchronized Intervention getParent() throws RemoteException {
		return _parent;
	}

      	public synchronized GregorianCalendar getDateDemande() throws RemoteException {
		return (GregorianCalendar) _dateDemande.clone();
	}
        
        public synchronized String getDateDemandeString() throws RemoteException {
                String date = null;
                SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");
                date = form.format(this._dateDemande.getTime());
                return date;
        }
        
        public synchronized String getDateInterventionString() throws RemoteException {
                String date = null;
                SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");
                date = form.format(this._dateIntervention.getTime());
                return date;
	}
	
  	public synchronized GregorianCalendar getDateIntervention() throws RemoteException {
                return _dateIntervention;
	}

        public synchronized Technicien getTechnicien() throws RemoteException, TechnicienMinimumMultiplicityException {
                if (!aUnTechnicienValide()) throw new TechnicienMinimumMultiplicityException();
                return _technicien;
	}

        public synchronized ITechnicien getITechnicien() throws RemoteException, TechnicienMinimumMultiplicityException {
                if (!aUnTechnicienValide()) throw new TechnicienMinimumMultiplicityException();
                return _technicien;
	}        
        
        public synchronized String getPrioriteIntervention() throws RemoteException {
            return _prioriteIntervention;
        }
        
        public GregorianCalendar getHeureDebut() throws RemoteException {
            return this._heureDebut;
        }
        
	public synchronized String getHeureDebutString() throws RemoteException {
            String heure = null;
            if (this._heureDebut!= null) {
                SimpleDateFormat form = new SimpleDateFormat("h:mm a");
                heure = form.format(this._heureDebut.getTime());
            }
            return heure;
	}

        public synchronized GregorianCalendar getHeureFin() throws RemoteException {
		return _heureFin;
	}

        public synchronized String getDescriptionIntervention() throws RemoteException {
		return _descriptionIntervention;
	}

        public synchronized Intervention getEnfant() throws RemoteException {
		return _enfant;
	}

	//*******************************************************************
        //Gestion des Statuts de l'intervention
        //*******************************************************************
        
        public boolean setInterventionEtatcree() throws RemoteException {
                InterventionEtat old = this._statut;
                this._statut = this._cree;
                    if(this._statut.setInterventionEtat(this)){
                        return true;
                    }
                    else {
                        this._statut = old;
                        return false;
                    }
        }
        
        public boolean setInterventionEtatencours() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._encours;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
                
        public boolean setInterventionEtatplanifiee() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._planifiee;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
        
        public boolean setInterventionEtatdeplanifiee() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._deplanifiee;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
        
        public boolean setInterventionEtatsuivis() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._suivis;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
        
        public boolean setInterventionEtatcloturee() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._cloturee;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
        
        public boolean setInterventionEtatannulee() throws RemoteException {
                //InterventionEtat old = this._statut;
                this._statut = this._annulee;
                //    if(this._statut.setInterventionEtat(this)){
                        return true;
                //    }
                //    else {
                //        this._statut = old;
                //        return false;
                //    }
        }
            
        //*******************************************************************
	//Associations
	//*******************************************************************

        //Adresse
        public boolean enregistrerAdresse(Adresse adresse){
                if (adresse == null) return false;
                if (this._adresse == adresse) return false;
                if (!peutEnregistrerAdresse())return false;
                this._adresse = adresse;
                return true;
        }
    
        //On ne peut pas supprimer l'adresse d'une intervention
        
        public boolean peutEnregistrerAdresse(){
                return this._adresse == null;
        }
    
        public boolean aUneAdresseValide(){
                return this._adresse != null;
        }
    
        //Client
        public boolean enregistrerClient(Client client) throws RemoteException{
                if (client == null) return false;
                if (this._client == client) return false;
                if (!peutEnregistrerClient())return false;
                if (!client.peutAjouterIntervention()&& !client.contientIntervention(this))return false;
                this._client = client;
                client.ajouterIntervention(this);
                return true;
        }
        
        public boolean supprimerClient() throws RemoteException {
                if (this._client == null) return false;
                Client tampon = this._client;
                this._client = null;
                tampon.supprimerIntervention(this);
                return true;
        }
        
        public boolean aUnClientValide(){
                return this._client != null;
        }
    
        public boolean peutEnregistrerClient(){
                return this._client == null;
        }
        
        //Intervention _parent
        //Association bidirectionelle role multiple
        public boolean ajouterEnfant(Intervention enfant) throws RemoteException {
                if (this._enfant == enfant) return false;
                if (!this.peutAjouterEnfant()) return false;
                if (!enfant.peutEnregistrerParent() && enfant.getParent() != this) return false;
                enregistrerEnfant(enfant);
                enfant.enregistrerParent(this);
                return true;
        }

        public boolean peutAjouterEnfant() {
	      return true;
	}

        //Association bidirectionelle role unaire
        public boolean enregistrerParent(Intervention parent) throws RemoteException{
                if (parent == null) return false;
                if (this._parent == parent) return false;
                if (!peutEnregistrerParent())return false;
                if (!parent.peutAjouterEnfant())return false;
                this._parent = parent;
                parent.ajouterEnfant(this);
                return true;
        }
        
        //On ne peut pas supprimer l'intervention parent d'une intervention
        
        public boolean aUnParentValide(){
                return this._parent != null;
        }
    
        public boolean peutEnregistrerParent(){
                return this._parent == null;
        }
        
        //Intervention _enfant
        //Association bidirectionelle role le multiple
        public boolean ajouterParent(Intervention parent) throws RemoteException {
                if (this._parent == parent) return false;
                if (!this.peutAjouterParent()) return false;
                if (!parent.peutEnregistrerEnfant() && parent.getEnfant() != this) return false;
                enregistrerParent(parent);
                parent.enregistrerEnfant(this);
                return true;
        }

        public boolean peutAjouterParent() {
	      return true;
	}

        //Association bidirectionelle role le unaire
        public boolean enregistrerEnfant(Intervention enfant) throws RemoteException{
                if (enfant == null) return false;
                if (this._enfant == enfant) return false;
                if (!peutEnregistrerEnfant())return false;
                if (!enfant.peutAjouterParent())return false;
                this._enfant = enfant;
                enfant.ajouterEnfant(this);
                return true;
        }
        
        //On ne peut pas supprimer l'intervention enfant d'une intervention
        
        public boolean aUnEnfantValide(){
                return this._parent != null;
        }
    
        public boolean peutEnregistrerEnfant(){
                return this._parent == null;
        }
        
        //Technicien
        public boolean enregistrerTechnicien(Technicien technicien) {
                if (this._technicien == technicien) return false;
		//if (!peutEnregistrerClient()) return false;
                if (!peutEnregistrerIntervention()) return false;
                if (!technicien.peutAjouterIntervention() && !technicien.contientIntervention(this)) return false;
                this._technicien = technicien;
		technicien.ajouterIntervention(this);
		return true;
	}

        public boolean supprimerTechnicien() {
                if (this._technicien == null) return false;
                Technicien tampon = this._technicien;
                this._technicien = null;
                tampon.supprimerIntervention(this);
                return true;
        }

        public boolean peutEnregistrerIntervention() {
                return this._technicien == null;
	}

        public boolean aUnTechnicienValide() {
		return true;
	}
        
        //Lister
        public synchronized static Vector<Intervention> lister() throws RemoteException, InterventionInexistantException, TechnicienMinimumMultiplicityException {
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().lister();
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0; i < listeInterventionDto.size(); i++) {
                    InterventionDto interventionDto = listeInterventionDto.get(i);
                    Intervention intervention = new Intervention(interventionDto);
                    listeIntervention.add(intervention);
                }
                return listeIntervention;
        }
        
        public synchronized static Vector<Intervention> rechercher(Client client) throws RemoteException, InterventionInexistantException, AdresseMinimumMultiplicityException, CodePostalMinimumMultiplicityException, ZoneMinimumMultiplicityException {
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().rechercherHistorique(((ClientParticulier)client).creerClientParticulierDto());
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0; i < listeInterventionDto.size(); i++) {
                    InterventionDto interventionDto = listeInterventionDto.get(i);
                    Intervention intervention = new Intervention(interventionDto);
                    listeIntervention.add(intervention);
                }
                return listeIntervention;
        }
        
        //Modifier
        public synchronized static void modifier(Intervention intervention) throws  RemoteException, InterventionInexistantException, 
                                                                                    AdresseInexistantException, 
                                                                                    CodePostalMinimumMultiplicityException, 
                                                                                    ZoneMinimumMultiplicityException {
                AbstractFactory.getInstance().getInterventionDao().modifier(intervention.creerInterventiondto());
        }

        //Planifier une intervention cree
        public void planifier(  Technicien technicien, GregorianCalendar dateIntervention, String prioriteIntervention) throws  
                                RemoteException, InterventionInexistantException,
                                AdresseInexistantException, 
                                AdresseMinimumMultiplicityException, 
                                CodePostalMinimumMultiplicityException, 
                                ZoneMinimumMultiplicityException, RoleMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventionPlanifieedto();
                TechnicienDto technicienDto = technicien.creerTechnicienDto();
                interventionDto.setTechnicien(technicienDto);
                interventionDto.setDateIntervention(dateIntervention);
                interventionDto.setPrioriteIntervention(new PrioriteDto(String.valueOf(prioriteIntervention.charAt(0))));
                FactoryImpl.getInstance().getInterventionDao().planifier(interventionDto);
                this.setInterventionEtatplanifiee();
                this.enregistrerTechnicien(technicien);
                this.setDateIntervention(dateIntervention);
                this.setPrioriteIntervention(prioriteIntervention);
                //notifyAll();
        }

        //Replanifier une intervention cree
        public void replanifier(Technicien technicien, GregorianCalendar dateIntervention)  throws  RemoteException, InterventionInexistantException,
                                                                                                    AdresseInexistantException, 
                                                                                                    AdresseMinimumMultiplicityException, 
                                                                                                    CodePostalMinimumMultiplicityException, 
                                                                                                    ZoneMinimumMultiplicityException, RoleMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventiondto();
                TechnicienDto technicienDto = technicien.creerTechnicienDto();
                interventionDto.setTechnicien(technicienDto);
                interventionDto.setDateIntervention(dateIntervention);
                FactoryImpl.getInstance().getInterventionDao().replanifier(interventionDto);
                this.setInterventionEtatplanifiee();
                this.enregistrerTechnicien(technicien);
                this.setDateIntervention(dateIntervention);
                //notifyAll();
        }

        //Déplanifier une intervention planifiee
        public void deplanifier()  throws   RemoteException, InterventionInexistantException,
                                            AdresseInexistantException, 
                                            CodePostalMinimumMultiplicityException, 
                                            ZoneMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventiondto();
                interventionDto.setTechnicien(null);
                FactoryImpl.getInstance().getInterventionDao().deplanifier(interventionDto);
                this.setInterventionEtatdeplanifiee();
                this.supprimerTechnicien();
                //this.enregistrerTechnicien(null);
        }

        //Traiter une intervention
        public void traiter(GregorianCalendar heureDebut)  throws   RemoteException, InterventionInexistantException,
                                                                    AdresseInexistantException, 
                                                                    CodePostalMinimumMultiplicityException, 
                                                                    ZoneMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventiondto();
                interventionDto.setHeureDebut(heureDebut);
                interventionDto.setTechnicien(null);
                FactoryImpl.getInstance().getInterventionDao().traiter(interventionDto);
                this.setInterventionEtatencours();
                this.setHeureDebut(heureDebut);
        }

        //Cloturer une intervention
        public void cloturer(String descriptionIntervention, GregorianCalendar heureFin) throws RemoteException, InterventionInexistantException,
                                                                                                AdresseInexistantException, 
                                                                                                CodePostalMinimumMultiplicityException, 
                                                                                                ZoneMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventiondto();
                interventionDto.setDescriptionIntervention(descriptionIntervention);
                interventionDto.setHeureFin(heureFin);
                FactoryImpl.getInstance().getInterventionDao().cloturer(interventionDto);
                this.setInterventionEtatcloturee();
                ListeInterventions.getInstance().supprimerIntervention(this);
        }

        //Annuler une intervention
        public void annuler()   throws  RemoteException, InterventionInexistantException,
                                        AdresseInexistantException, CodePostalMinimumMultiplicityException, 
                                        ZoneMinimumMultiplicityException {
                InterventionDto interventionDto = this.creerInterventiondto();
                FactoryImpl.getInstance().getInterventionDao().annuler(interventionDto);
                ListeInterventions.getInstance().supprimerIntervention(this);
         }
        
	public static synchronized Vector<Intervention> listeInterventionHistorique() throws RemoteException, InterventionInexistantException{
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().listerHistorique();
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0 ; i < listeInterventionDto.size() ; i++) {
                    Intervention intervention = new Intervention(listeInterventionDto.get(i));
                    listeIntervention.add(intervention);
                }
                return listeIntervention;
        }
        
	public static synchronized Vector<Intervention> listeInterventionSuivies() throws RemoteException, InterventionInexistantException{
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().listerHistorique();
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0 ; i < listeInterventionDto.size() ; i++) {
                    Intervention intervention = new Intervention(listeInterventionDto.get(i));
                    if(intervention.getStatut().getlibelle().contentEquals("EN")) {
                    listeIntervention.add(intervention);}
                }
                return listeIntervention;
        }
        
	public static synchronized Vector<Intervention> listeInterventionAnnulees() throws RemoteException, InterventionInexistantException{
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().listerHistorique();
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0 ; i < listeInterventionDto.size() ; i++) {
                    Intervention intervention = new Intervention(listeInterventionDto.get(i));
                    if(intervention.getStatut().getlibelle().contentEquals("AN")) {
                    listeIntervention.add(intervention);}
                }
                return listeIntervention;
        }
        
        public static synchronized Vector<Intervention> listeInterventionCloturees() throws RemoteException, InterventionInexistantException{
                Vector<InterventionDto> listeInterventionDto = AbstractFactory.getInstance().getInterventionDao().listerHistorique();
                Vector<Intervention> listeIntervention = new Vector<Intervention>();
                for (int i = 0 ; i < listeInterventionDto.size() ; i++) {
                    Intervention intervention = new Intervention(listeInterventionDto.get(i));
                    if(intervention.getStatut().getlibelle().contentEquals("CL")) {
                    listeIntervention.add(intervention);}
                }
                return listeIntervention;
        }        

        //*******************************************************************
	//Method Clonable
	//*******************************************************************        

        @Override
        public Object clone() throws CloneNotSupportedException {
                Intervention intervention = null;
                intervention = (Intervention) super.clone();
                intervention._adresse = (Adresse) _adresse.clone();
                intervention._client = (Client) _client.clone();
                return intervention;
        }

}